<?php
/**
 * 注解标签基类
*/

namespace houze\route\doc\lable;

class Basics
{
    /**
     * 初始化请求
    */
    protected $server;
    protected $fd;
    protected $request;
    protected $response;
    protected $websocket_server;
    protected $websocket_response;
    protected $websocket_data;
    protected $controller_instance;
    protected $controller_method;

    public function __construct($server=null, $fd=null) {
        // 获取容器
        $this->server = $server;
        $this->fd = $fd;
        $this->request = \houze\context\Request::get();
        $this->response = \houze\context\Response::get();
        $this->websocket_server = \houze\context\Container::get('websocket_server');
        $this->websocket_response = \houze\context\Container::get('websocket_response');
        $this->websocket_data = \houze\context\Container::get('websocket_data');
        $this->controller_instance = \houze\context\Container::get('controller_instance');
        $this->controller_method = \houze\context\Container::get('controller_method');
    }

    // 需要主动更新容器
    protected function _return($return=true) {
        \houze\context\Request::set($this->request);
        \houze\context\Response::set($this->response);
        \houze\context\Container::set('websocket_server', $this->websocket_server);
        \houze\context\Container::set('websocket_response', $this->websocket_response);
        \houze\context\Container::set('websocket_data', $this->websocket_data);
        \houze\context\Container::set('controller_instance', $this->controller_instance);
        \houze\context\Container::set('controller_method', $this->controller_method);
        return $return;
    }

    /**
     * 当注解Param检测失败时，回调的处理函数
    */
    protected function param_error_callback($callback, $tips, $name, $status, $attach=null) {
        // 若为单元测试调试，则直接通过
        if (
            (!empty($this->request->get['HouzeTestCase'])) || 
            (!empty($this->request->post['HouzeTestCase']))
        ) {
            return true;
        }
		
		\houze\Lifecycle::annotate_param($this->server, $this->fd, $callback, $tips, $name, $status, $attach);
        
        return false;
    }

    /**
     * 当其余注解检测失败时，回调的处理函数
    */
    protected function route_error($status) {
        // 若为单元测试调试，则直接通过
        if (
            (!empty($this->request->get['HouzeTestCase'])) || 
            (!empty($this->request->post['HouzeTestCase']))
        ) {
            return true;
        }
		
        \houze\Lifecycle::route_error($this->server, $this->fd, $status);
        
        return false;
    }
}